package com.enniswu.weitter.widget.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.enniswu.weitter.R;
import com.enniswu.weitter.browse.BrowseActivity;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.AlbumViewHolder> {

    private List<String> album;
    private boolean isShowDelete;
    private OnItemClickListener clickListener = new SimpleOnItemClickListener();

    public AlbumAdapter(List<String> album) {
        this.album = album;
    }

    public void setAlbum(List<String> album) {
        this.album = album;
    }

    public void setShowDelete(boolean showDelete) {
        isShowDelete = showDelete;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public AlbumViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_album, parent, false);
        final AlbumViewHolder holder = new AlbumViewHolder(view);

        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener == null)
                    return;
                int position = holder.getAdapterPosition();
                switch (v.getId()) {
                    case R.id.iv_delete:
                        clickListener.onDeleteClick(v, position);
                        break;
                    default:
                        clickListener.onItemClick(v, position);
                        break;
                }
            }
        };
        view.setOnClickListener(l);
        view.findViewById(R.id.iv_delete).setOnClickListener(l);
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (clickListener == null)
                    return false;
                int position = holder.getAdapterPosition();
                clickListener.onItemLongClick(v, position);
                return true;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AlbumViewHolder holder, int position) {
        Glide.with(holder.ivImage).load(album.get(position)).into(holder.ivImage);
        if (isShowDelete) {
            holder.ivDelete.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return album.size();
    }

    static class AlbumViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_image)
        ImageView ivImage;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        public AlbumViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener extends com.enniswu.weitter.widget.adapter.OnItemClickListener {
        void onItemLongClick(View v, int position);
        void onDeleteClick(View v, int position);
    }

    public class SimpleOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemLongClick(View v, int position) {
        }

        @Override
        public void onDeleteClick(View v, int position) {
        }

        @Override
        public void onItemClick(View v, int position) {
            Intent intent = new Intent(v.getContext(), BrowseActivity.class);
            intent.putExtra("album", (Serializable) album);
            intent.putExtra("position", position);
            v.getContext().startActivity(intent);
        }
    }

    public class MultiOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemLongClick(View v, int position) {
            setShowDelete(true);
            notifyDataSetChanged();
        }

        @Override
        public void onDeleteClick(View v, int position) {
            album.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, album.size() - position);
        }

        @Override
        public void onItemClick(View v, int position) {
            Intent intent = new Intent(v.getContext(), BrowseActivity.class);
            intent.putExtra("album", (Serializable) album);
            intent.putExtra("position", position);
            v.getContext().startActivity(intent);
        }
    }
}
